//
//  registerViewController.m
//  Copy_AiKapPu_Personal_OC
//
//  Created by taojianz on 2017/7/4.
//  Copyright © 2017年 taojianz. All rights reserved.
//

#import "registerViewController.h"
#import <nbappdev/nbappdev.h>

@interface registerViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *avatarSelectionBtn;
@property (weak, nonatomic) IBOutlet UIButton *maleBtn;
@property (weak, nonatomic) IBOutlet UIButton *famaleBtn;
@property (weak, nonatomic) IBOutlet UITextField *birthdayTextField;
@property (weak, nonatomic) IBOutlet UITextField *regionTextField;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (strong,nonatomic) ToastView *toastView;
@property (strong,nonatomic)UIToolbar *birthdayToolbar;
@property (strong,nonatomic)UIDatePicker *birthdayPicker;
@property (strong,nonatomic)UIPickerView *regionPicker;
@property (strong,nonatomic)UIToolbar *regionToolbar;
@property (assign,nonatomic)BOOL isUserChosenAvatat;
@property (strong,nonatomic)NSString *gender;



@end

@implementation registerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    _toastView = [[ToastView alloc]init];
    _isUserChosenAvatat = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)chooseAvatar{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]  initWithTitle:@"需上传本人真实照片"
                                                    delegate:self
                                                    cancelButtonTitle:@"取消"
                                                    destructiveButtonTitle:nil
                                                    otherButtonTitles:@"拍照",@"从相册选取", nil];
    [actionSheet showInView:self.view];
    
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex  {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    
    switch (buttonIndex) {
            // camera
        case 0:
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self presentViewController:picker animated:true completion:nil];
            }
            break;
        case 1:
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
                picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                [self presentViewController:picker animated:true completion:nil];
            }
            break;
        default:
            break;
    }
}
// 选中图片之后操作
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    // 获取到修改之后的图片
    UIImage *editedImage =[info objectForKey:UIImagePickerControllerEditedImage];
    
    NSData *imageDate = UIImageJPEGRepresentation(editedImage, 0.3);
    [self.avatarSelectionBtn setImage:editedImage forState:UIControlStateNormal];
    [picker dismissViewControllerAnimated:true completion:nil];
    _isUserChosenAvatat = YES;
}
// 选择性别

- (IBAction)sexBtnTapped:(UIButton *)sender {
    switch (sender.tag) {
        case 100:
            self.famaleBtn.selected = NO;
            self.gender = @"male";
            break;
        case 101:
            self.maleBtn.selected = NO;
            self.gender = @"famale";
            break;
        default:
            break;
    }
    sender.selected = !sender.selected;
    if (![self.famaleBtn isSelected]&& ![self.maleBtn isSelected]){
        self.gender = nil;
    }
    
}

// MARK: 设置UI相关
-(void)setupUI{
    self.navigationController.navigationBarHidden = NO;
    [_avatarSelectionBtn addTarget:self action:@selector(chooseAvatar) forControlEvents:UIControlEventTouchUpInside];
    _avatarSelectionBtn.layer.cornerRadius = _avatarSelectionBtn.frame.size.height/2;
    _avatarSelectionBtn.clipsToBounds = true;
    [self setupTextField];
}
-(void)setupTextField{
    NSLocale *local = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    // 设置生日的inputView
    self.birthdayPicker =  [[UIDatePicker alloc] init];
    
    _birthdayPicker.minimumDate = [[NSDate alloc] initWithTimeIntervalSince1970:-666665656];
    _birthdayPicker.maximumDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
    _birthdayPicker.datePickerMode = UIDatePickerModeDate;
    _birthdayPicker.locale = local;
    [_birthdayPicker addTarget:self action:@selector(birthdayPickerValueChanged) forControlEvents:UIControlEventValueChanged];
    
    self.birthdayToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 40)];
    
    UIBarButtonItem *birthdayToolbarFinishBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(resignBirthdayKeyboard)];
    UIBarButtonItem *spaceBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                            target:nil
                                                                                            action:nil];
    [_birthdayToolbar setItems:[NSArray arrayWithObjects:spaceBarItem,birthdayToolbarFinishBtn, nil]];
    
    self.birthdayTextField.delegate = self;
    self.birthdayTextField.inputView = _birthdayPicker;
    self.birthdayTextField.inputAccessoryView = _birthdayToolbar;
    
    [self.view endEditing:true];
    
    // 设置地区的inputView
    self.regionPicker = [[UIPickerView alloc] init];
    
    _regionPicker.delegate = self;
    _regionPicker.dataSource = self;
    _regionPicker.backgroundColor = UIColor.whiteColor;
    
    _regionToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 40)];
    
    UIBarButtonItem *regionToolbarFinishBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(resignBirthdayKeyboard)];
    
    [_birthdayToolbar setItems:[NSArray arrayWithObjects:spaceBarItem,regionToolbarFinishBtn, nil]];
    
    
}

-(void)resignRegionKeyboard{
    
    
    
    [self.view endEditing:true];
    
}

-(void)resignBirthdayKeyboard{
    
    // MARK:获取datePicker的选中日期
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    self.birthdayTextField.text =[dateformatter stringFromDate:self.birthdayPicker.date];
    
    
    [self.view endEditing:true];
    
}
-(void)birthdayPickerValueChanged{
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    self.birthdayTextField.text =[dateformatter stringFromDate:self.birthdayPicker.date];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 0;
}
- (IBAction)nextStep:(UIButton *)sender {
    
    NSString *userName = self.userNameTextField.text;
    NSString *birthday = self.birthdayTextField.text;
    NSString *region = self.regionTextField.text;

    if (!_isUserChosenAvatat){
        [_toastView makeToast:@"请选择您的头像" toastState:ToastWarning duration:0.8 didFinishBlock:nil];
        return;
    }

    if (_gender == nil){//若用户未选择性别则gender为nil
        [_toastView makeToast:@"请选择您的性别" toastState:ToastWarning duration:0.8 didFinishBlock:nil];
        return ;
    }
    if ([userName isEqualToString:@""]){
        [_toastView makeToast:@"请填写您的昵称" toastState:ToastWarning duration:0.8 didFinishBlock:nil];
        return ;
    }if ([birthday isEqualToString:@""]){
        [_toastView makeToast:@"请选择您的生日" toastState:ToastWarning duration:0.8 didFinishBlock:nil];
        return ;
    }if ([region isEqualToString:@""]){
        [_toastView makeToast:@"请选择您的地区" toastState:ToastWarning duration:0.8 didFinishBlock:nil];
        return ;
    }
    
    id phoneRegisterVC = [Helper getViewController:@"phoneRegisterViewController" withStoryboard:@"Login"];
    [self.navigationController pushViewController:phoneRegisterVC animated:true];
    
}

@end

